package bairui.international.weixin.portal.miniapp.controller.order.manage;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;

import bairui.international.model.domain.jpa.TB_BranchCompanyStaff;
import bairui.international.model.domain.jpa.TB_User;
import bairui.international.service.Service_BranchCompanyStaff;
import bairui.international.service.Service_ProductOrder;
import bairui.international.service.Service_User;

/**
 *
 * 完成
 *
 * @generated
 * 
 * @see STest_C_Order_Manage_Finish_Get 测试
 *
 */
@Controller
public class C_Order_Manage_Finish_Get
 extends SC_Order_Manage_Finish_Get 
{

	@Autowired private Service_ProductOrder service_ProductOrder;
	@Autowired private Service_User service_User;
	@Autowired private Service_BranchCompanyStaff service_BranchCompanyStaff;
	
	/**
	 * Get请求
	 */
	@Action(Get.class)
	@GetMapping(Get.RequestMappingValue)
	public @FomatOutput @ResponseBody Object get(
		ModelMap map, HttpServletRequest request, 
		@PathVariable(name=Get.PathVariableName.Id) long id,
		@RequestParam String skey
	) throws Exception {
		/*
		 * 只有被指派的员工可以提交验证
		 */
		TB_User user = service_User.findOne(it->it.skey.eq(skey));
		TB_BranchCompanyStaff branchCompanyStaff = service_BranchCompanyStaff.getActive(user);
		if(null == branchCompanyStaff){
			return null;
		}
		if(service_ProductOrder.exists(it->
			it.id.eq(id)
				.and(it.branchCompany.eq(branchCompanyStaff.getBranchCompany()))
				.and(it.sender.user.eq(user))
			)
		){
			service_ProductOrder.finish(id);
		}
		return null;
	}
	

}
